package com.china08.yxyapi.api.schwebsite.index;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;

/**
 * 
 * @annotation: 站内统计
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController("SwsIndexSiteCountController")
public class SwsIndexSiteCountController extends YxyApiBaseController {

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Autowired
    private SwsSectionRepository swsSectionRepository;

    @Autowired
    private SwsArticleRepository swsArticleRepository;

    @RequestMapping(value = "/api_yw/schwebsite/index/site/count/sch", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);
        String pid = "0";
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains DomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != DomainName, "域名对象为空");
        String schoolId = DomainName.getSchoolId();
        boolean isVisible = true;
        boolean showNavigation = true;
        List<SwsSection> bySchoolIdAndPid = swsSectionRepository
                .findBySchoolIdAndIsVisibleAndShowNavigationAndPidOrderByOrderNumAsc(schoolId, isVisible,
                        showNavigation, pid);

        List<RespModel> respModels = new ArrayList<RespModel>();

        for (SwsSection swsSection : bySchoolIdAndPid) {
            RespModel model = new RespModel();
            List<String> section1List = new ArrayList<String>();
            List<String> section2List = new ArrayList<String>();

            String id = swsSection.getId();
            section1List.add(id);
            String section1Id = swsSection.getId();
            List<SwsSection> sectionList = swsSectionRepository
                    .findBySchoolIdAndIsVisibleAndShowNavigationAndPidOrderByOrderNumAsc(schoolId, isVisible,
                            showNavigation, section1Id);

            for (SwsSection swsSection2 : sectionList) {
                String id2 = swsSection2.getId();
                section2List.add(id2);
            }
            Integer section1Num = swsArticleRepository.countByOwnerSchoolIdAndAuditStatusAndSection1IdIn(schoolId, 1,
                    section1List);
            model.sectionName = swsSection.getSectionName();
            model.articleNum = section1Num;
            respModels.add(model);
        }

        return respModels;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {
        // 栏目名称
        public String sectionName;
        // 栏目对应文章数
        public Integer articleNum;
    }

}
